<?php

/*
 +-----------------------------------------------------------------------+
 | plugins/sieverules/sieverules_func.inc                                |
 |                                                                       |
 | This file is part of the RoundCube Webmail client                     |
 | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland                 |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | PURPOSE:                                                              |
 |	 Provide common functionality for SieveRule plugin                   |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: Aleksander Machniak <alec@alec.pl>                            |
 | Modifications by: Philip Weir                                         |
 +-----------------------------------------------------------------------+

 $Id: $

*/

require_once('plugins/sieverules/config.inc.php');
require_once('plugins/sieverules/Net_Sieve.php');
require_once('plugins/sieverules/rcube_sieve.php');
require_once('plugins/sieverules/rcube_sieve_script.php');

function sieverules_init() {
	global $RCMAIL, $OUTPUT, $sieverules_config, $SIEVE, $SIEVE_ERROR, $SCRIPT;

	// try to connect to managesieve server and to fetch the script
	$SIEVE = new rcube_sieve($_SESSION['username'],
				$RCMAIL->decrypt_passwd($_SESSION['password']),
				$sieverules_config['managesieve_host'],
				$sieverules_config['managesieve_port'],
				$sieverules_config['usetls'],
				$sieverules_config['ruleset_name']);

	$SIEVE_ERROR = $SIEVE->error();

	if ($SIEVE_ERROR == SIEVE_ERROR_NOT_EXISTS) {
		// if script not exists build default script contents
		if (!empty($CONFIG['managesieve_default']) && is_readable($CONFIG['managesieve_default']))
			$SIEVE->script->add_text(file_get_contents($CONFIG['managesieve_default']));

		// that's not exactly an error
		$SIEVE_ERROR = false;
	}
	elseif ($SIEVE_ERROR) {
		switch ($SIEVE_ERROR) {
			case SIEVE_ERROR_CONNECTION:
			case SIEVE_ERROR_LOGIN:
				$OUTPUT->show_message('filterconnerror', 'error');
			break;
			default:
				$OUTPUT->show_message('filterunknownerror', 'error');
			break;
		}

		$OUTPUT->set_env('sieveruleserror', true);
	}

	// finally set script objects
	if ($SIEVE_ERROR)
		$SCRIPT = array();
	else
		$SCRIPT = $SIEVE->script->as_array();
}

function rcmail_sieverules_list($attrib) {
	global $RCMAIL, $OUTPUT, $SCRIPT;
	$OUTPUT->add_gui_object('sieveruleslist', 'sieverules-table');

	sieverules_init();

	if (!empty($attrib['upicon']))
		$up_button = html::img(array('src' => $RCMAIL->config->get('skin_path') . $attrib['upicon'], 'alt' => rcube_label('moveup'), 'border' => 0));
	else
		$up_button = rcube_label('moveup');

	if (!empty($attrib['downicon']))
		$down_button = html::img(array('src' => $RCMAIL->config->get('skin_path') . $attrib['downicon'], 'alt' => rcube_label('movedown'), 'border' => 0));
	else
		$down_button = rcube_label('movedown');

	$table = new html_table(array('id' => 'sieverules-table', 'class' => 'records-table', 'cellspacing' => '0', 'cols' => 2));
	$table->add_header(array('colspan' => 2), rcube_label('filters'));

	if (sizeof($SCRIPT) == 0) {
		$table->add(array('colspan' => '2'), rep_specialchars_output(rcube_label('nosieverules')));
		$table->add_row();
	}
	else foreach($SCRIPT as $idx => $filter) {
		$table->set_row_attribs(array('id' => 'rcmrow' . $idx));

		if ($filter['disabled'] == 1)
			$table->add(null, $filter['name'] . ' (' . rcube_label('disabled') . ')');
		else
			$table->add(null, $filter['name']);

		$up_link = html::a(array('href' => "#", 'onclick' => 'return sieverule_up('.$idx.')', 'title' => rcube_label('moveup')), $up_button);
		$down_link = html::a(array('href' => "#", 'onclick' => 'return sieverule_down('.$idx.')', 'title' => rcube_label('movedown')), $down_button);

		$table->add('control', $up_link . '&nbsp;' . $down_link);
	}

	$out = $table->show();

	return $out;
}

// coppied from rcube_imap.php
function mod_mailbox($mbox_name, $mode='in') {
	global $RCMAIL, $sieverules_config;

	if (!$sieverules_config['include_imap_root'])
		return $mbox_name;

	if ((!empty($RCMAIL->imap->root_ns) && $RCMAIL->imap->root_ns == $mbox_name) || $mbox_name == 'INBOX')
		return $mbox_name;

	if (!empty($RCMAIL->imap->root_dir) && $mode=='in')
		$mbox_name = $RCMAIL->imap->root_dir.$RCMAIL->imap->delimiter.$mbox_name;
	else if (strlen($RCMAIL->imap->root_dir) && $mode=='out')
		$mbox_name = substr($mbox_name, strlen($RCMAIL->imap->root_dir)+1);

	return $mbox_name;
}

?>
